home *** CD-ROM | disk | FTP | other *** search
-
- #include <LibAUX.h>
- #include </:usr:include:sys:errno.h>
- #include </:usr:include:sys:signal.h>
- #include </:usr:include:fcntl.h>
- #include <StdIO.h>
- #include <Memory.h>
- #include <CursorCtl.h>
-
- #define AUXNL (char) '\012'
- #define MACOSRET (char) '\015'
-
-
- #define STACKSIZE 2048
-
- pascal long AUXDispatch(selector,p)
- short selector;
- char *p;
- extern 0xABf9;
-
- #define AUX_GET_ENVIRON 11 /* get pointer to environ */
-
- static sigfunc_t cstat, istat, qstat, mstat, signal();
- static char *childstack;
- static int pid, pipefd_stdout[2], pipefd_stderr[2];
-
-
- int
- auxsystem(s)
- char *s;
- {
- int status,n, **fake=((int *)&s)+1;
- int GetAUXErrno(), AUXisRunning(), mysigint();
- char *malloc();
- char **auxenviron;
-
- if (!AUXisRunning())
- {
- printf("A/UX is not running \n");
- return(-1);
- }
-
- childstack = NewPtr(STACKSIZE);
-
- /* copy the environment */
- AUXDispatch(AUX_GET_ENVIRON,(char *)&auxenviron);
-
- if (auxpipe(pipefd_stdout) == -1 || auxpipe(pipefd_stderr) == -1)
- {
- (void)printf("Error in creating pipes %d \n", GetAUXErrno());
- return(-1);
- }
-
- cstat = auxsignal(SIGCLD, SIG_DFL);
- istat = auxsignal(SIGINT, SIG_IGN);
- qstat = auxsignal(SIGQUIT, SIG_IGN);
- mstat = signal(SIGINT, mysigint); /* this is MPW's signal handler */
-
- if((pid = auxfork(childstack+STACKSIZE,fake)) == 0) {
- if (auxclose(1) == -1)
- return(-1);
- (void)auxdup(pipefd_stdout[1]);
- if (auxclose(2) == -1)
- return(-1);
- (void)auxdup(pipefd_stderr[1]);
- if ((auxclose(pipefd_stdout[0]) == -1) ||
- (auxclose(pipefd_stdout[1]) == -1)) return(-1);
- if ((auxclose(pipefd_stderr[0]) == -1) ||
- (auxclose(pipefd_stderr[1]) == -1)) return(-1);
- pid = auxgetpid();
- (void) auxsetpgrp(pid,pid);
- (void) auxexecl("/bin/sh", "sh", "-c", s, 0);
- aux_exit(127);
- }
- else {
- if (pid < 0) {
- (void)auxclose(pipefd_stdout[0]);
- (void)auxclose(pipefd_stdout[1]);
- (void)auxclose(pipefd_stderr[0]);
- (void)auxclose(pipefd_stderr[1]);
- (void) auxsignal(SIGCLD, cstat);
- (void) auxsignal(SIGINT, istat);
- (void) auxsignal(SIGQUIT, qstat);
- (void) signal(SIGINT, mstat);
- DisposPtr((char *)childstack); /* Fork failed */
- return(-1);
- }
- else {
- char *cp, buf[1024];
- long cnt;
- int flags,out,err;
-
- flags = auxfcntl(pipefd_stdout[0],F_GETFL,0);
- (void)auxfcntl(pipefd_stdout[0],F_SETFL,flags | O_NONBLOCK);
- (void)auxclose(pipefd_stdout[1]);
- flags = auxfcntl(pipefd_stderr[0],F_GETFL,0);
- (void)auxfcntl(pipefd_stderr[0],F_SETFL,flags | O_NONBLOCK);
- (void)auxclose(pipefd_stderr[1]);
-
- for ( out=err=1; out || err; ) {
- if ( out ) cnt = auxread(pipefd_stdout[0],buf,1024);
- else cnt = 0;
- if ( cnt > 0 ) {
- buf[cnt] = '\0';
- while ( cp = (char *)strchr(buf,AUXNL) )
- *cp = MACOSRET;
- (void)write(fileno(stdout), buf, cnt);
- }
- else {
- if ( cnt == 0 ) out = 0;
- if ( err ) cnt = auxread(pipefd_stderr[0],buf,1024);
- else cnt = 0;
- if ( cnt > 0 ) {
- buf[cnt] = '\0';
- while ( cp = (char *)strchr(buf,AUXNL) )
- *cp = MACOSRET;
- (void)write(fileno(stderr), buf, cnt);
- }
- else if ( cnt == 0 ) err = 0; SpinCursor(1);
- };
- SpinCursor(1);
- };
-
- for ( status=n=0; n!=pid; n=auxwait(&status) )
- if ( n<0 && GetAUXErrno()!=EINTR ) break;
-
- auxclose(pipefd_stdout[0]);
- auxclose(pipefd_stderr[0]);
-
- (void) auxsignal(SIGCLD, cstat);
- (void) auxsignal(SIGINT, istat);
- (void) auxsignal(SIGQUIT, qstat);
- (void) signal(SIGINT, mstat);
- DisposPtr((char *)childstack);
- return(status);
- }
- }
- }
-
- mysigint()
- {
- int status,n;
-
- if ( pid > 0 ) {
- (void) auxkill(-pid,SIGKILL);
- for ( status=n=0; n!=pid; n=auxwait(&status) )
- if ( n<0 && GetAUXErrno()!=EINTR ) break;
- };
- auxclose(pipefd_stdout[0]);
- auxclose(pipefd_stderr[0]);
- (void) auxsignal(SIGCLD, cstat);
- (void) auxsignal(SIGINT, istat);
- (void) auxsignal(SIGQUIT, qstat);
- (void) signal(SIGINT, mstat);
- DisposPtr((char *)childstack);
- printf("Unixcmd execution terminated by user interrupt\n");
- exit(-1);
- }
-